import json

jsonStr=open("article.postman_collection.json",'r+',encoding='utf-8')
doc=json.load(jsonStr)
jsonStr.close()

groups = []
for x in doc["item"]:
    groups.append(x)


with open('output.txt','w') as output:
    for group in groups:
        for item in group["item"]:
            groupName=group["name"]
            itemName=item["name"]
            if not ("response" in item):
                continue
            resp=item["response"][0]
            req=resp["originalRequest"]
            method=req["method"]
            url=req["url"]["raw"]
            querys=req["url"]["query"] if "query" in req["url"] else []
            params=[]
            for query in querys:
                params.append({"key":query["key"],"value":query["value"]})
            respbody=json.dumps(json.loads(resp["body"],encoding="utf-8"),ensure_ascii=False,indent=4)
            output.writelines(itemName + "\n")
            output.writelines("请求地址: " + url +'\n')
            output.writelines("请求方法： " + method + '\n')
            output.writelines("请求参数： \n")
            output.writelines("key|value|是否必填|备注\n")
            output.writelines("---|---|---|---\n")
            for x in params:
                output.writelines(x["key"] + "|" + x["value"] + "\n")
            output.writelines("回复：\n")
            output.writelines(respbody + "\n\n")
        output.writelines('\n')
